<template>
  <div class="homePage-content active-list-page">
    <div class="brands-page">
      <header class="page-header">
        <div class="container">
          <h1 class="page-title">出海品牌榜单</h1>
          <p class="page-subtitle">汇集全球最具价值和影响力的企业榜单</p>
        </div>
      </header>

      <div class="container">
        <!-- 筛选区域 -->
        <div class="filter-container">
          <div class="filter-row">
            <span class="filter-label">类型</span>
            <div class="filter-options">
              <div v-for="(type, index) in brandCategories" :key="type" class="filter-option"
                :class="{ active: selectedType === type.title }" @click="selectedType = type.title">
                {{ type.title }}
              </div>
            </div>
          </div>
        </div>
        <!-- 榜单分类展示 -->
        <div v-for="(category) in filteredCategories" :key="category.title" class="ranking-category">
          <h2 class="category-title">{{ category.title }}</h2>
          <div class="ranking-grid">
            <div v-for="item in category.items" :key="item.id" class="ranking-card-link" @click="goBrandDetailsFun(item)">
              <div class="ranking-card">
                <div class="ranking-image" :style="{ backgroundColor: item.color }">
                  <h2 class="line-clamp-2">{{ item.title }}</h2>
                  <br>
                  <h2 class="line-clamp-2">{{ item.englishTitle }}</h2>
                  <div class="ranking-badge">{{ item.badge }}</div>
                </div>
                <div class="ranking-content">
                  
                  <div class="ranking-meta">
                    <span class="line-clamp-2">发布机构: {{ item.author }}</span>
                    <span>
                      <DateConvert :date="item.createTime" />
                    </span>
                  </div>
                  <div class="ranking-tags">
                    <span v-for="tag in item.tags" :key="tag" class="ranking-tag">
                      {{ tag }}
                    </span>
                  </div>
                </div>
              </div>
            </div>
          </div>  
          <!-- 分页控件 -->
          <div class="pagination">
            <div class="total">共{{ category.params.total }}项数据</div>
            <el-pagination style="justify-content: center" background layout="prev, pager, next" 
            :total="category.params.total"
              v-model:current-page="category.params.pageNum"
              v-model:page-size="category.params.pageSize"
               @current-change="getBrandListFun(category)" />
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, computed, onMounted } from 'vue'
import { getBrandList, getBrandCategories,incrementBrandViewCount } from '@/api/channel'

onMounted(() => {
  getBrandCategoriesFun();
})

const brandCategories = ref([])

const colorList = [
  '#3358D0',
  '#FF6B25',
  '#19BC93',
  '#24C5EC',
]

// 跳转品牌详情
const goBrandDetailsFun = (category, index) => {
  incrementBrandViewCount(category.id);
  console.log(category)
  // 根据link跳转新页面
  window.open(category.link, '_blank')
}

// 获取品牌分类
const getBrandCategoriesFun = async () => {
  const res = await getBrandCategories();
  if (res.suc) {
    brandCategories.value = res.data.map((item, index) => ({
      ...item,
      title: item.categoryName,
      items: [],
      // 依次取颜色列表,每种颜色使用一次,四次一个循环
      color: colorList[index % colorList.length],
      params: {
        pageNum: 1,
        pageSize: 4,
        total: 0,
        categoryId: item.categoryId
      }
    }));
    // 头部放一个title = 全部   
    brandCategories.value.unshift({
      title: '全部',
      items: [],
    })
    brandCategories.value.forEach((item, index) => {
      getBrandListFun(item, index)
    })

  }
}

// const brandList = ref([])

// 获取品牌列表
const getBrandListFun = async (item) => {
  if(item.title === '全部'){
    return
  }
  const res = await getBrandList(item.params)
  if (res.suc) {
    item.items = res.data.list.map((listItem, itemIndex) => ({
      ...listItem,
      color: colorList[itemIndex % colorList.length]
    }));
    item.params.total = res.data.total;
  }
}



// 筛选类型
const selectedType = ref('全部')

// 分页
const currentPage = ref(1)
const totalPages = ref(3)

// 榜单数据
const categories = [
  {
    title: '品牌价值',
    items: [
      {
        id: 1,
        title: '世界500强\n(Fortune Global 500)',
        name: '世界500强',
        publisher: '《财富》杂志',
        date: '2025-02-15',
        badge: '知名品牌',
        bgClass: 'bg-blue',
        tags: ['品牌价值', '全球', '综合实力']
      },
      {
        id: 2,
        title: '《中国500最具价值品牌》',
        name: '中国500最具价值品牌',
        publisher: '世界品牌实验室',
        date: '2025-06-22',
        badge: '知名品牌',
        bgClass: 'bg-orange',
        tags: ['品牌价值', '中国', '价值评估']
      },
      // ... 其他品牌价值榜单
    ]
  },
  {
    title: '雇主品牌',
    items: [
      {
        id: 5,
        title: '《福布斯全球最佳雇主》',
        name: '福布斯全球最佳雇主',
        publisher: '福布斯',
        date: '2025-05-18',
        badge: '雇主品牌',
        bgClass: 'bg-purple',
        tags: ['雇主品牌', '全球', '人力资源']
      },
      // ... 其他雇主品牌榜单
    ]
  },
  {
    title: 'ESG',
    items: [
      {
        id: 9,
        title: '《Global 100 Most Sustainable Corporations》',
        name: '全球100家最可持续发展公司',
        publisher: 'Corporate Knights',
        date: '2025-01-20',
        badge: 'ESG',
        bgClass: 'bg-green',
        tags: ['ESG', '全球', '可持续发展']
      },
      // ... 其他ESG榜单
    ]
  },
  {
    title: '创新能力',
    items: [
      {
        id: 12,
        title: '《全球最具创新力企业50强》',
        name: '全球最具创新力企业50强',
        publisher: '波士顿咨询',
        date: '2025-01-10',
        badge: '创新能力',
        bgClass: 'bg-purple',
        tags: ['创新能力', '全球', '技术创新']
      },
      // ... 其他创新能力榜单
    ]
  }
]

// 根据筛选条件过滤分类
const filteredCategories = computed(() => {
  if (selectedType.value === '全部') {
    return brandCategories.value.filter(item => item.title !== '全部')
  }
  return brandCategories.value.filter(item => item.title === selectedType.value)
})
</script>

<style scoped>
.brands-page {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
  background: #f5f7fa;
  padding: 10px 0;
  margin: 10px 0;
  color: #333;
  line-height: 1.6;
  border-radius: 10px;
}

.container {
  max-width: 1200px;
  margin: 0 auto;
  padding: 0 15px;
}

.page-header {
  background: linear-gradient(135deg, #4a6bdf, #2a51ca);
  padding: 40px 0;
  margin-bottom: 30px;
  color: white;
  border-radius: 10px;
  text-align: center;
}

.page-title {
  font-size: 28px;
  font-weight: 600;
  margin-bottom: 10px;
}

.page-subtitle {
  font-size: 16px;
  opacity: 0.9;
}

.filter-container {
  background: #fff;
  border-radius: 10px;
  padding: 20px;
  margin-bottom: 30px;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.05);
}

.filter-row {
  display: flex;
  align-items: center;
  margin-bottom: 15px;
}

.filter-label {
  font-weight: 500;
  width: 60px;
  flex-shrink: 0;
  color: #555;
}

.filter-options {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
}

.filter-option {
  padding: 6px 15px;
  background: #f5f5f5;
  border-radius: 20px;
  font-size: 14px;
  cursor: pointer;
  transition: all 0.3s;
}

.filter-option:hover {
  background: #e6f7ff;
  color: #1890ff;
}

.filter-option.active {
  background: #1890ff;
  color: white;
}

.ranking-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(250px, 1fr));
  gap: 20px;
  margin-bottom: 30px;
  cursor: pointer;
}

.ranking-card {
  background: #fff;
  border-radius: 10px;
  overflow: hidden;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.05);
  transition: all 0.3s;
  height: 100%;
  display: flex;
  flex-direction: column;
}

.ranking-card:hover {
  transform: translateY(-5px);
  box-shadow: 0 5px 15px rgba(0, 0, 0, 0.1);
}

.ranking-image {
  height: 160px;
  position: relative;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  overflow: hidden;
}

.ranking-image h2 {
  color: white;
  text-align: center;
  font-size: 20px;
  z-index: 2;
  padding: 0 15px;
  font-weight: 600;
}

.ranking-badge {
  position: absolute;
  top: 10px;
  right: 10px;

  color: #1890ff;
  font-size: 12px;
  padding: 4px 8px;
  border-radius: 4px;
  font-weight: 500;
}

.ranking-content {
  padding: 15px;
  flex-grow: 1;
  display: flex;
  flex-direction: column;
  background: rgba(255, 255, 255, 1);
}

.ranking-title {
  font-size: 18px;
  margin-bottom: 0px;
  font-weight: 600;
  color: #333;
}

.ranking-meta {
  display: flex;
  flex-direction: column;
  color: #888;
  font-size: 14px;
  margin-bottom: 10px;
}

.ranking-tags {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-top: auto;
}

.ranking-tag {
  background: #f5f5f5;
  padding: 4px 10px;
  border-radius: 4px;
  font-size: 12px;
  color: #666;
}

.pagination {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-top: 30px;
  margin-bottom: 50px;
}

.pagination-list {
  display: flex;
  list-style: none;
}

.pagination-item {
  margin: 0 4px;
}

.pagination-link {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 36px;
  height: 36px;
  border-radius: 4px;
  border: 1px solid #d9d9d9;
  color: #666;
  text-decoration: none;
  transition: all 0.3s;
}

.pagination-link:hover,
.pagination-link.active {
  background: #1890ff;
  color: white;
  border-color: #1890ff;
}

.ranking-category {
  margin-bottom: 40px;
}

.category-title {
  font-size: 22px;
  font-weight: 600;
  color: #333;
  margin-bottom: 20px;
  padding-bottom: 12px;
  border-bottom: 1px solid #e8e8e8;
  position: relative;
}

.category-title:after {
  content: "";
  position: absolute;
  left: 0;
  bottom: -1px;
  width: 50px;
  height: 3px;
  background: #1890ff;
  border-radius: 3px;
}

/* 背景色类 */
.bg-blue {
  background: linear-gradient(135deg, #4a6bdf, #2a51ca);
}

.bg-orange {
  background: linear-gradient(135deg, #ff7d3b, #ff4e00);
}

.bg-green {
  background: linear-gradient(135deg, #26c9a0, #00a278);
}

.bg-purple {
  background: linear-gradient(135deg, #a462e3, #7030a0);
}

.bg-teal {
  background: linear-gradient(135deg, #2ccef5, #00a0c6);
}

.bg-red {
  background: linear-gradient(135deg, #ff5571, #e62e4d);
}

.bg-yellow {
  background: linear-gradient(135deg, #ffc54d, #f59f00);
}

.bg-cyan {
  background: linear-gradient(135deg, #4ecdc4, #26a69a);
}

/* 动画效果 */
@keyframes fadeInUp {
  from {
    opacity: 0;
    transform: translateY(20px);
  }

  to {
    opacity: 1;
    transform: translateY(0);
  }
}

.ranking-card {
  animation: fadeInUp 0.5s ease-out forwards;
}

.ranking-card:nth-child(1) {
  animation-delay: 0.1s;
}

.ranking-card:nth-child(2) {
  animation-delay: 0.2s;
}

.ranking-card:nth-child(3) {
  animation-delay: 0.3s;
}

.ranking-card:nth-child(4) {
  animation-delay: 0.4s;
}

.ranking-card:nth-child(5) {
  animation-delay: 0.5s;
}

.ranking-card:nth-child(6) {
  animation-delay: 0.6s;
}

.ranking-card:nth-child(7) {
  animation-delay: 0.7s;
}

.ranking-card:nth-child(8) {
  animation-delay: 0.8s;
}

@media (max-width: 768px) {
  .filter-row {
    flex-direction: column;
    align-items: flex-start;
  }

  .filter-label {
    margin-bottom: 10px;
  }

  .ranking-grid {
    grid-template-columns: repeat(auto-fill, minmax(220px, 1fr));
  }
}
</style>
